<template>
	<div class="home" id="home">
		<img @click="fenshiFunction" src="../assets/logo.png">
		<h1 @click="showmoder">基座-home page</h1>
		<h1 @click="interrupt">任务中断和恢复</h1>
		<h1 @click="testFun">测试函数</h1>
	</div>
</template>

<script>
	import showMsg from '@/utils/general.js'
	import {performChunk,browserPermChunk} from '@/utils/sharingFunction.js'
	import {addTask,processTasks,testFuns}from'@/utils/concurrent.js'
	export default {
		name: 'Home',
		components: {
			
		},
		data() {
			return {
				dom:null,
				tasks:[],
				recovery:true
			}
		},
		mounted() {
			this.arithmetic()
			this.datas = new Array(100000).fill(0).map((_, i)=> i);
			
			// this.concurrents()//并发任务
			this.requesList()//生成任务列表
		},
		methods: {
			requesList(){
				for(let i = 0; i < 5; i++){
					this.tasks.push(()=>new Promise((resolve,reject)=>{
						setTimeout(()=>{
							if(i == 1){
								reject(`失败${i + 1}`)
							}else{
								resolve(`成功${i + 1}`)
							}
						},2000)
					}))
				}
				this.processor = processTasks(...this.tasks)
			},
			testFun(){
				testFuns()
			},
			async interrupt(){//任务中断与恢复
				if(this.recovery){
					console.log('点击开始')
					this.recovery = !this.recovery
					const result = await this.processor.start()
					console.log('任务执行完成',result)
				}else{
					console.log('点击暂停')
					this.recovery = !this.recovery
					const result = await this.processor.pause()
				}
			},
			concurrents(){//并发任务
				addTask(1000,1)
				addTask(1000,2)
				addTask(1000,3)
				addTask(1000,4)
				addTask(1000,5)
			},
			fenshiFunction(){//分时函数
				let dom = document.getElementById('home')
				const taskHandle = (_,i)=>{
					const div = document.createElement('div');
					div.innerText = i;
					dom.appendChild(div)
				}
				const scheduler = (task) =>{
					//如果一秒执行一次
					setTimeout(()=>{
						const now = performance.now()
						task(()=> performance.now() - now <= 10);
					},1000)
				}
				performChunk(100000,taskHandle,scheduler)
				// browserPermChunk(100000,taskHandle)
			},
			arithmetic(){
				let READ = 0b1;
				let WETIE = 0b10;
				let UPDATE = 0b100; 
				let DELETE = 0b1000;
				let xx = READ | DELETE;
				// console.log(xx,'444444444444',UPDATE)
			},
			showmoder(){
				showMsg('显示的消息',(close)=>{
					console.log('点击了确定')
					close()
				})
			}
		},
	}
</script>

<style>
</style>